import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        List<int[]> shops = new ArrayList<>();
        Set<Integer> varieties = new HashSet<>();

        for (int i = 0; i < N; i++) {
            int xi = scanner.nextInt();
            int pi = scanner.nextInt();
            shops.add(new int[]{xi, pi});
            varieties.add(pi);
        }

        // 按位置排序
        shops.sort((a, b) -> Integer.compare(a[0], b[0]));

        int totalVarieties = varieties.size();
        Map<Integer, Integer> windowCounts = new HashMap<>();
        int left = 0;
        int minLength = Integer.MAX_VALUE;
        int currentVarieties = 0;

        for (int right = 0; right < N; right++) {
            int pi = shops.get(right)[1];
            windowCounts.put(pi, windowCounts.getOrDefault(pi, 0) + 1);
            if (windowCounts.get(pi) == 1) {
                currentVarieties++;
            }

            while (currentVarieties == totalVarieties) {
                int currentLength = shops.get(right)[0] - shops.get(left)[0];
                if (currentLength < minLength) {
                    minLength = currentLength;
                }
                int leftPi = shops.get(left)[1];
                windowCounts.put(leftPi, windowCounts.get(leftPi) - 1);
                if (windowCounts.get(leftPi) == 0) {
                    currentVarieties--;
                }
                left++;
            }
        }

        System.out.println(minLength);
    }
}
